﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace Webapi.EFCore.Repository.Base
{
    public class BaseRepository<TEntity> where TEntity : class, new()
    {
        public EFCoreDbContext EFCoreDb;
        public BaseRepository(EFCoreDbContext efCoreDb)
        {
            EFCoreDb = efCoreDb;
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public void Add(TEntity model)
        {
            EFCoreDb.Add(model);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public async Task<List<TEntity>> GetList()
        {
            return await EFCoreDb.Set<TEntity>().ToListAsync();
        }

        /// <summary>
        /// 通过ID(反填)
        /// </summary>
        /// <returns></returns>
        public async Task<TEntity> GetBYID(int id)
        {
            return await EFCoreDb.Set<TEntity>().FindAsync(id);
        }
        /// <summary>
        /// 异步提交
        /// </summary>
        /// <returns></returns>
        public async Task<int> SaveChangesAsync()
        {
            return await EFCoreDb.SaveChangesAsync();
        }

        /// <summary>
        /// 同步提交
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return EFCoreDb.SaveChanges();
        }
    }
}
